﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using DAL;

namespace BIZ
{
    public class ServiceOrderBIZ
    {
        public void Add(SERVICEORDER sERVICEORDER)
        {
            WebUtils.db.SERVICEORDERs.InsertOnSubmit(sERVICEORDER);
            WebUtils.db.SubmitChanges();
        }

        public void Edit(SERVICEORDER order)
        {
            var _order = WebUtils.db.SERVICEORDERs.Single(o => o.ServiceID.Equals(order.ServiceOrderID));

            _order.CustomerName = order.CustomerName;
            _order.CustomerPhone = order.CustomerPhone;
            _order.CustomerEmail = order.CustomerEmail;
            _order.CustomerAddress = order.CustomerAddress;
            _order.Note = order.Note;

            WebUtils.db.SubmitChanges();
        }

        public SERVICEORDER getId(Guid id)
        {
            var o = (from order in WebUtils.db.SERVICEORDERs
                     where order.ServiceOrderID.Equals(id)
                    select order).FirstOrDefault();

            return o;
        }

        public List<SERVICEORDER> GetAll(int status)
        {
            return WebUtils.db.SERVICEORDERs.ToList();
        }

        public List<SERVICEORDER> GetAll(int status, DateTime fromDate, DateTime toDate)
        {
            IQueryable<SERVICEORDER> list;
            if (status == 0)
            {
                list = (from order in WebUtils.db.SERVICEORDERs where order.CreateDate >= fromDate && order.CreateDate <= toDate orderby order.CreateDate select order);
            }
            else
            {
                list = (from order in WebUtils.db.SERVICEORDERs where order.Status == (status == 1 ? true : false) && order.CreateDate >= fromDate && order.CreateDate <= toDate orderby order.CreateDate select order);
            }

            return list.ToList();
        }

        public void ChangeStatus(Guid id, bool status)
        {
            SERVICEORDER p = WebUtils.db.SERVICEORDERs.SingleOrDefault(order => order.ServiceOrderID.Equals(id));
            p.Status = status;

            WebUtils.db.SubmitChanges();
        }
    }
}
